<template>
  <div class="app-container">
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-row>
          <el-button type="success" @click="handleCreate">&emsp;新增&emsp;</el-button>
          <!-- <el-button type="danger" @click="handleDeleteAll">批量删除</el-button> -->
        </el-row>
      </div>
      <div v-loading="listLoading" class="item">
        <el-table ref="dataList" :data="menuList" :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
                  border row-key="id"
                  style="width:100%;margin-bottom: 20px;"
        >
          <!-- <el-table-column type="selection"></el-table-column> -->
          <el-table-column label="序号" prop="id"></el-table-column>
          <el-table-column label="名称" prop="name"></el-table-column>
          <el-table-column label="一阶段" prop="condition1"></el-table-column>
          <el-table-column label="二阶段" prop="condition2"></el-table-column>
          <el-table-column label="三阶段" prop="condition3"></el-table-column>
          <el-table-column label="一阶段(图)" prop="frame1">
            <template slot-scope="scope">
              <el-popover max-width="420" placement="right" trigger="hover">
                <img :src="scope.row.frame1" alt="" style="max-width: 200px;max-height: 200px">
                <img slot="reference" :src="scope.row.frame1" alt="" style="width: 50px;">
              </el-popover>
            </template>
          </el-table-column>
          <el-table-column label="二阶段(图)" prop="frame2">
            <template slot-scope="scope">
              <el-popover max-width="420" placement="right" trigger="hover">
                <img :src="scope.row.frame2" alt="" style="max-width: 200px;max-height: 200px">
                <img slot="reference" :src="scope.row.frame2" alt="" style="width: 50px;">
              </el-popover>
            </template>
          </el-table-column>
          <el-table-column label="三阶段(图)" prop="frame3">
            <template slot-scope="scope">
              <el-popover max-width="420" placement="right" trigger="hover">
                <img :src="scope.row.frame3" alt="" style="max-width: 200px;max-height: 200px">
                <img slot="reference" :src="scope.row.frame3" alt="" style="width: 50px;">
              </el-popover>
            </template>
          </el-table-column>
          <!-- <el-table-column prop="add_time" label="操作时间"> </el-table-column> -->
          <el-table-column label="默认评分" prop="default_score"></el-table-column>
          <el-table-column label="一星评分" prop="one_star_score"></el-table-column>
          <el-table-column label="二星评分" prop="two_star_score"></el-table-column>
          <el-table-column label="三星评分" prop="three_star_score"></el-table-column>
          <el-table-column label="操作" width="180px">
            <template slot-scope="scope">
              <el-button icon="el-icon-edit" plain size="mini" type="primary" @click="handleUpdate(scope.row)">编辑
              </el-button>
              <el-button icon="el-icon-delete" plain size="mini" type="danger" @click="handleDelete(scope.row)">删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <cus-pagination
          :limit.sync="listQuery.perPage"
          :page.sync="listQuery.page"
          :total="total"
          @pagination="getList"
        />
      </div>
    </el-card>
    <!-- 删除弹框 -->
    <el-dialog
      :visible.sync="deleteDialogVisible"
      title="确定删除吗?"
      width="435px"
    >
      <span style=""><em style="font-style:normal;color:red">删除后不可恢复</em>，你还要继续吗？</span>
      <span slot="footer" class="dialog-footer">
            <el-button @click="deleteDialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="confirmDelete">确 定</el-button>
        </span>
    </el-dialog>
    <!-- 新增编辑弹框 -->
    <!-- http://admin-api.yutangwl.com/upload/image/add -->
    <el-dialog v-dialogDrag :title="titleMap[dialogStatus]" :visible.sync="dialogFormVisible">
      <el-form ref="editRulesForm" :model="editRulesForm" :rules="editRulesFormRules" class="demo-ruleForm"
               label-width="120px"
      >
        <el-form-item label="名称" prop="name">
          <el-input v-model="editRulesForm.name" placeholder="请输入名称"></el-input>
        </el-form-item>
        <el-form-item label="一阶段:"></el-form-item>
        <el-form-item label="数量">
          <el-input-number v-model="editRulesForm.condition1" :min="1" placeholder="请输入>=1的数"></el-input-number>
        </el-form-item>
        <el-form-item label="图片" prop="">
          <el-upload
            ref="upload"

            :before-upload="beforeAvatarUpload1"
            :file-list="picture_list_1"
            :on-change="handleChangePic1"
            :on-remove="handlePictureRemove1"
            action="#"
            class="upload-demo"
            list-type="picture"
          >
            <el-button slot="trigger" size="small" type="primary">上传图片</el-button>
          </el-upload>
        </el-form-item>
        <el-form-item label="二阶段:"></el-form-item>
        <el-form-item label="数量">
          <el-input-number v-model="editRulesForm.condition2" :min="1" placeholder="请输入>=1的数"></el-input-number>
        </el-form-item>
        <el-form-item label="图片" prop="">
          <el-upload
            ref="upload"

            :before-upload="beforeAvatarUpload2"
            :file-list="picture_list_2"
            :on-change="handleChangePic2"
            :on-remove="handlePictureRemove2"
            action="#"
            class="upload-demo"
            list-type="picture"
          >
            <el-button slot="trigger" size="small" type="primary">上传图片</el-button>
          </el-upload>
        </el-form-item>
        <el-form-item label="三阶段:"></el-form-item>
        <el-form-item label="数量">
          <el-input-number v-model="editRulesForm.condition3" :min="1" placeholder="请输入>=1的数"></el-input-number>
        </el-form-item>
        <el-form-item label="图片" prop="">
          <el-upload
            ref="upload"

            :before-upload="beforeAvatarUpload3"
            :file-list="picture_list_3"
            :on-change="handleChangePic3"
            :on-remove="handlePictureRemove3"
            action="#"
            class="upload-demo"
            list-type="picture"
          >
            <el-button slot="trigger" size="small" type="primary">上传图片</el-button>
          </el-upload>
        </el-form-item>
        <el-form-item label="默认评分" prop="default_score">
          <el-input-number v-model="editRulesForm.default_score" :min="1" placeholder="请输入"></el-input-number>
        </el-form-item>
        <el-form-item label="一星评分" prop="one_star_score">
          <el-input-number v-model="editRulesForm.one_star_score" :min="1" placeholder="请输入"></el-input-number>
        </el-form-item>
        <el-form-item label="二星评分" prop="two_star_score">
          <el-input-number v-model="editRulesForm.two_star_score" :min="1" placeholder="请输入"></el-input-number>
        </el-form-item>
        <el-form-item label="三星评分" prop="three_star_score">
          <el-input-number v-model="editRulesForm.three_star_score" :min="1" placeholder="请输入"></el-input-number>
        </el-form-item>
        <el-form-item>
          <el-button @click="dialogFormVisible = false">返回</el-button>
          <el-button type="primary" @click="submitForm('editRulesForm')">提交</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {
  listData,
  postAddData,
  putEditData,
  deleteData
} from '@/api/giftMem/giftWall'
import {
  getToken
} from '@/utils/auth'

export default {
  data() {
    return {
      confirmBtn: false,

      picture_list_1: [],
      picture_list_2: [],
      picture_list_3: [],

      // 要修改的当前行的id
      row_id: 0,
      // 黑名单类别
      blackList: [
        { value: 1, label: '1' },
        { value: 2, label: '4' },
        { value: 3, label: '6' },
        { value: 4, label: '3' },
        { value: 5, label: '5' }
      ],
      // 选中的黑名单类别
      checkBlackList: [],
      provinceList: [],
      cityList: [],
      // 表单规则
      editRulesFormRules: {
        name: [{ required: true, message: '请输入名称', trigger: 'blur' }],
      },

      editRulesForm: {
        name: undefined,
        condition1: undefined,
        condition2: undefined,
        condition3: undefined,
        frame1: undefined,
        frame2: undefined,
        frame3: undefined,
        default_score: undefined,
        one_star_score: undefined,
        two_star_score: undefined,
        three_star_score: undefined,
      },
      dialogStatus: '',
      // dialog的头部信息
      titleMap: {
        update: '编辑',
        create: '添加'
      },
      dialogFormVisible: false,
      deleteDialogVisible: false,
      listLoading: false,
      menuList: [],
      limitList: [],
      listQuery: {
        page: 1,
        perPage: 10,
      },
      total: 0,
      // 勾选内容
      multipleSelection: [],
      // 要删除的内容的ids
      dataIds: '',
    }
  },
  created() {
    this.getList()
  },
  methods: {
    handleChangePic1(file, fileList) {
      this.picture_list_1 = [{ url: file.url }]
      if (fileList.length > 1) {
        fileList.splice(0, 1);
      }
    },
    handleChangePic2(file, fileList) {
      this.picture_list_2 = [{ url: file.url }]
      if (fileList.length > 1) {
        fileList.splice(0, 1);
      }
    },
    handleChangePic3(file, fileList) {
      this.picture_list_3 = [{ url: file.url }]
      if (fileList.length > 1) {
        fileList.splice(0, 1);
      }
    },
    beforeAvatarUpload1(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;
      this.editRulesForm.frame1 = file
    },
    handlePictureRemove1(res) {
      this.editRulesForm.frame1 = undefined;
      this.picture_list_1 = []
    },
    beforeAvatarUpload2(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;
      this.editRulesForm.frame2 = file
    },
    handlePictureRemove2(res) {
      this.editRulesForm.frame2 = undefined;
      this.picture_list_2 = []
    },
    beforeAvatarUpload3(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;
      this.editRulesForm.frame3 = file
    },
    handlePictureRemove3(res) {
      this.editRulesForm.frame3 = undefined;
      this.picture_list_3 = []
    },
    getList() {
      this.listLoading = true;
      let urlData = "?";
      if (this.listQuery.page) {
        urlData = urlData + `page=${this.listQuery.page}&`;
      }
      if (this.listQuery.perPage) {
        urlData = urlData + `perPage=${this.listQuery.perPage}`;
      }
      listData(urlData).then((response) => {
        this.menuList = response.data.list;
        this.total = response.data.meta.total;
        this.listLoading = false
        this.confirmBtn = false
      })
    },
    // 点击新增
    handleCreate() {
      this.resetForm('editRulesForm')
      this.dialogStatus = 'create'
      this.dialogFormVisible = true
    },
    // 点击编辑
    handleUpdate(row) {
      this.row_id = row.id
      this.editRulesForm.name = row.name;
      this.editRulesForm.condition1 = row.condition1 ? row.condition1 : undefined;
      this.editRulesForm.condition2 = row.condition2 ? row.condition2 : undefined;
      this.editRulesForm.condition3 = row.condition3 ? row.condition3 : undefined;
      this.editRulesForm.default_score = row.default_score
      this.editRulesForm.one_star_score = row.one_star_score
      this.editRulesForm.two_star_score = row.two_star_score
      this.editRulesForm.three_star_score = row.three_star_score

      if (row.frame1) {
        this.editRulesForm.frame1 = row.frame1
        this.picture_list_1 = [{ url: row.frame1 }]
      }
      if (row.frame2) {
        this.editRulesForm.frame2 = row.frame2
        this.picture_list_2 = [{ url: row.frame2 }]
      }
      if (row.frame3) {
        this.editRulesForm.frame3 = row.frame3
        this.picture_list_3 = [{ url: row.frame3 }]
      }

      this.dialogStatus = 'update'
      this.dialogFormVisible = true
    },
    // 重置表单内容
    resetForm(formName) {
      this.$nextTick(() => {
        // this.$refs[formName].resetFields();
        if (this.$refs[formName] !== undefined) {
          this.$refs[formName].resetFields();
        }
        this.editRulesForm = {
          name: undefined,
          condition1: undefined,
          condition2: undefined,
          condition3: undefined,
          frame1: undefined,
          frame2: undefined,
          frame3: undefined,
          default_score: undefined,
          one_star_score: undefined,
          two_star_score: undefined,
          three_star_score: undefined,
        }
        this.picture_list_1 = []
        this.picture_list_2 = []
        this.picture_list_3 = []
      })

    },

    submitForm(formName) {
      if (this.confirmBtn) return

      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.confirmBtn = true
          // alert('submit!');
          if (this.dialogStatus === 'create') {
            let formData = new FormData();
            for (let item in this.editRulesForm) {
              this.editRulesForm[item] === undefined ? '' : formData.append(item, this.editRulesForm[item]);
            }
            postAddData(formData).then(response => {
              if (response.code === 200) {
                this.getList()
                this.dialogFormVisible = false;
                this.$notify({
                  type: 'success',
                  message: '操作成功',
                  position: 'bottom-right',
                  showClose: false,
                  duration: 3000
                });
              } else {
                this.confirmBtn = false
              }
            })
          } else {
            let obj = { id: this.row_id }
            for (let key in this.editRulesForm) {
              this.editRulesForm[key] === undefined ? '' : (obj[key] = this.editRulesForm[key])
            }
            let formData = new FormData();
            for (let item in obj) {
              obj[item] === undefined ? '' : formData.append(item, obj[item]);
            }
            putEditData(formData).then(response => {
              if (response.code === 200) {
                this.getList()
                this.dialogFormVisible = false;
                this.$notify({
                  type: 'success',
                  message: '操作成功',
                  position: 'bottom-right',
                  showClose: false,
                  duration: 3000
                });
              } else {
                this.confirmBtn = false
              }
            })
          }
        } else {
          return false;
        }
      });
    },
    // 勾选
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    // 点击单个删除
    handleDelete(row) {
      this.dataIds = { ids: `${row.id}` }
      this.deleteDialogVisible = true
    },
    // 点击批量删除
    handleDeleteAll() {
      if (this.multipleSelection.length === 0) {
        this.$message({
          message: '请先勾选要删除的内容',
          type: 'warning'
        });
        return;
      } else if (this.multipleSelection.length !== 0) {
        let ids = '';
        for (let key in this.multipleSelection) {
          ids += `${this.multipleSelection[key].id},`
        }
        ids = ids.substring(0, ids.length - 1)
        this.dataIds = { ids: ids }
      }
      this.deleteDialogVisible = true
    },
    // 确定删除
    confirmDelete() {
      if (this.confirmBtn) return
      this.confirmBtn = true
      deleteData(this.dataIds).then((response) => {
        if (response.code === 200) {
          this.getList()
          this.deleteDialogVisible = false;
          this.$notify({
            type: 'success',
            message: '操作成功',
            position: 'bottom-right',
            showClose: false,
            duration: 3000
          });
        } else {
          this.confirmBtn = false
        }
      })
    },
  }
}
</script>

<style scoped>
/deep/ .el-upload-list__item {
  transition : none !important;
}
</style>
